<!-- components/ToastMessage.vue -->
<template>
  <Teleport to="body">
    <!-- Toast内容 -->
    <div
      v-if="visible || isLeaving"
      class="toast-wrapper"
      :class="[
        `toast-${position}`,
        `toast-${type}`,
        { 'toast-active': visible && !isLeaving, 'toast-leave': isLeaving },
      ]"
    >
      <div class="toast-content">
        <!-- 标题区域 -->
        <div v-if="title" class="toast-header">
          <i v-if="icon" :class="['iconfont', icon]"></i>
          <span>{{ title }}</span>
        </div>
        <!-- 消息内容 -->
        <div class="toast-body">
          {{ message }}
        </div>
      </div>
    </div>
  </Teleport>
</template>

<script>
export default {
  name: "ToastMessage",
  props: {
    title: {
      type: String,
      default: "提示",
    },
    message: {
      type: String,
      required: true,
    },
    icon: {
      type: String,
      default: "",
    },
    duration: {
      type: Number,
      default: 2000,
    },
    position: {
      type: String,
      default: "center", // top | center | bottom
      validator: (val) => ["top", "center", "bottom"].includes(val),
    },
    type: {
      type: String,
      default: "info", // info | success | warning | error
      validator: (val) => ["info", "success", "warning", "error"].includes(val),
    },
  },
  data() {
    return {
      visible: false,
      isLeaving: false,
    };
  },
  emits: ["closed"],
  mounted() {
    setTimeout(() => {
      this.visible = true;
      this.autoClose();
    }, 50);
  },
  methods: {
    autoClose() {
      setTimeout(() => {
        this.startLeaveAnimation();
      }, this.duration);
    },
    startLeaveAnimation() {
      this.isLeaving = true;
      setTimeout(() => {
        this.visible = false;
        this.isLeaving = false;
        this.$emit("closed");
      }, 300);
    },
  },
};
</script>

<style scoped lang="less">
/* 基础样式 */
.toast-wrapper {
  position: fixed;
  left: 50%;
  opacity: 0;
  transform: translateX(-50%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 2000;
  will-change: transform, opacity;
}

/* 不同位置 */
.toast-top {
  top: 20px;
  transform: translateX(-50%) translateY(-20px);
}

.toast-center {
  top: 50%;
  transform: translate(-50%, -50%) translateY(20px);
}

.toast-bottom {
  bottom: 20px;
  transform: translateX(-50%) translateY(20px);
}

/* 激活状态 */
.toast-active {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast-center.toast-active {
  transform: translate(-50%, -50%) translateY(0);
}

/* 离开动画 */
.toast-leave {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
  transition: all 0.2s ease-in;
}

.toast-center.toast-leave {
  transform: translate(-50%, -50%) translateY(20px);
}

.toast-bottom.toast-leave {
  transform: translateX(-50%) translateY(20px);
}

/* 内容样式 */
.toast-content {
  padding: 16px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  max-width: 70vw;
  word-break: break-word;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;
  min-width: 200px;
  background-color: var(--v-background-pure);
  border-left: 4px solid;
}

/* 不同类型颜色 */
.toast-info {
  .toast-content {
    border-color: var(--v-sidebar-background);
  }
}
.toast-info .toast-header {
  color: var(--v-sidebar-background);
}

.toast-success {
  .toast-content {
    border-color: var(--v-success);
  }
}
.toast-success .toast-header {
  color: var(--v-success);
}

.toast-warning {
  .toast-content {
    border-left-color: var(--v-warning);
  }
}
.toast-warning .toast-header {
  color: var(--v-warning);
}

.toast-error {
  .toast-content {
    border-left-color: var(--v-danger);
  }
}
.toast-error .toast-header {
  color: var(--v-danger);
}

/* 头部样式 */
.toast-header {
  display: flex;
  align-items: center;
  flex-direction: column;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 16px;
}

.toast-header .iconfont {
  font-size: 34px;
}

/* 消息体样式 */
.toast-body {
  font-size: 14px;
  color: var(--v-text);
}
</style>